package efr.util;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import efr.dao.EmailDao;
import efr.dao.EmailGreetDao;
import efr.dao.impl.EmailDaoImpl;
import efr.dao.impl.EmailGreetDaoImpl;
import efr.entity.Email;
import efr.entity.EmailGreet;

public class HelloJob implements Job{
	
	private static Logger logger = Logger.getLogger(HelloJob.class);

	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		EmailGreetDao emailDao = new EmailGreetDaoImpl();
		try {
			EmailGreet email = emailDao.getEmailGreet4Newsletter();
			if (email == null) {
				throw new Exception("Email null");
			} else {
				EmailConfiguration.getInstance().sendMail(email);
				email.setConfirmare(0);
				emailDao.updateEmailGreet(email);
			}
			
		} catch (Exception e) {
			 JobExecutionException e2 = 
		                new JobExecutionException(e);
		            // Quartz will automatically unschedule
		            // all triggers associated with this job
		            // so that it does not run again
		            e2.setUnscheduleAllTriggers(true);
		            throw e2;
		}

		logger.debug("Ciao, Mr. Job!");
		
	}
	

}
